﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SETUPABON.xaml
    /// </summary>
    public partial class TDOCIMG
    {

        public TDOCIMG()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_FOLDERS();
            this.DataContext = MV;
            InitializeComponent();

            MV.OnMessage += MV_OnMessage;
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            ART = Helper.ApplicationPath.Replace("\\Exe", "\\Documents");
            Helper.CheckDirectory(Helper.ApplicationPath.Replace("\\Exe", ""), "Documents"); 
            base.Window_Loaded(sender, e);
        }
        private void MV_OnMessage(object sender, MessageEventArgs e)
        {
            TMV.INFO = e.Info.Details;
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                dwData = TBMain.NewRow();
                dwData["MANDANT"] = app.Mandant;
                dwData["SUB"] = " AND (SUB LIKE '%.jpg' OR SUB LIKE '%.jpg')";
                SResult = TMV.QFALL(dwData);
                MergeData(SResult, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            base.SaveCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DFOLDERS01(dwMaster);
            filter = "ROWNID='" + dwMaster["ROWNID"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
        public void Image_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!System.IO.File.Exists(PHOTOLINK))
            {
                return;
            }
            if (!System.IO.File.Exists(@"C:\WINDOWS\system32\mspaint.exe"))
            {

                return;
            }
            string PATH = "\"" + PHOTOLINK + "\"";
            ProcessStartInfo pinf = new ProcessStartInfo(@"C:\WINDOWS\system32\mspaint.exe", PATH);
            Process pro = Process.Start(pinf);
            //pro.Start();

        }

        public void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsImageMax = !IsImageMax;
        }

        public override void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            TMV.AUTOIMGDETECT(TMV.ART);
            base.bgWorker_DoWork(sender, e);
        }
        public override void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            IsBusy = false;
            vwMaster.View.MoveCurrentToFirst();
            base.bgWorker_RunWorkerCompleted(sender, e);
        }

        private void DetectCommand(object sender, RoutedEventArgs e)
        {
            IsBusy = true;
            bgWorker.RunWorkerAsync();
        }
    }
}
